﻿namespace HuwmanCode.Net

open System
open System.IO
open System.Text
open System.Threading
open System.Net
open System.Net.Sockets
open System.Diagnostics
open System.Collections.Generic

type Port = int
and public FtpServer(address:IPAddress, port:Port) =
    let listener = new TcpListener(address, port)
    do listener.Start()
       Async.Start(FtpServer.ListenForClients(listener))
    new () = new FtpServer(new IPAddress([|127uy;0uy;0uy;1uy|]), 21)

    static member private ListenForClients(listener:TcpListener) =
        async { do printfn "Listening for clients on thread %d" Thread.CurrentThread.ManagedThreadId
                let! client = listener.AcceptTcpClientAsync()
                do printfn "Client connected from remote endpoint %s" (client.Client.RemoteEndPoint.ToString())
                Async.Start(FtpServer.ListenForClients(listener))
                let connection = new FtpConnection(client)
                ignore connection }

    interface IDisposable with
        member x.Dispose() =
            listener.Stop()